import { AsyncStorage } from 'react-native';
import Request from "../utils/Request";
const STORAGE_KEY = "STORAGE_KEY_mark";
export default {
    namespace: 'mark',
    state: {
        list: []
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *fetchList({ payload }, { call, put, select }) {
            const data = yield call(Request.get, Request.getApi().markList);
            if (data) {
                yield put({
                    type: 'save',
                    payload: { list: data.data.map(item => (Object.assign({}, item, { label: item.name, value: item.id }))) }
                });
            }
        },
    },
    subscriptions: {
        setup({ dispatch }) {
        }
    },
};
